open Printf

let file = "Tabout.csv"

let oc = open_out file


let rec convertFloatListToStringList l = 
		List.rev(List.fold_left (fun li e -> (string_of_float e)::li) [] l )
		


	   
let rec printheads l  oc= 
	
		match l with
		[] -> ()
		| [a] -> print_string ((List.hd a) ^ "     ");fprintf oc "%s,\n" (List.hd a);()
		| hd::tl -> print_string ((List.hd hd) ^ "     "); fprintf oc "%s,\t" (List.hd hd) ;printheads tl oc
		   
		
		
		   
let rec removeTop l =
	match l with
		[] -> []
		| [a] -> (match a with 
			 [] -> []
			| [b]-> []
			| hd1::tl1 ->  [List.tl a]
			)
		| hd::tl ->  (match hd with 
				[] -> [] 
				| [a]-> []
				| hd1::tl1 -> List.tl hd::(removeTop tl)
			     )
		

let rec metafoo l sourcefile= print_endline "";
	let filename = sourcefile ^ ".csv";
					in
					let file = filename
			in
			
			let oc = open_out file
		in
	let rec metafoo1 l= 
	
	match l with 
	[] -> ()
	| hd::tl ->  printheads l oc; metafoo1 (print_endline"";removeTop l);()
	in
	match l with 
	[] -> ()
	| hd::tl ->  printheads l oc; metafoo1 (print_endline"";removeTop l);()
	
	

let rec maxlength l =
	let getMaxlength n li = 
		if List.length li > n  then List.length li
		else  n;
	in List.fold_left getMaxlength 0 l


let rec padLists l =
	let maxl = maxlength l
	in
	let rec padonelist  li =   (*print_string "Max"; print_int maxl;*)
	    if List.length li = maxl then li
	    else if List.length li < maxl then padonelist (li@["nodata"])
	    else raise (Failure ("maxlength not working"))
	in
	let foldfunc     retLi li=
		let ret = (padonelist li)::retLi
		in ret 
	in 
		List.fold_left foldfunc []  l
	
